﻿using PaperMaster.Chess.CardSpace;
using PaperMaster.Chess.ChessModel;
using PaperMaster.Chess.Enemy;
using PaperMaster.Chess.PlayerCore;
using PaperMaster.Info;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

namespace PaperMaster.Model
{
    /// <summary>
    /// 实现此接口，标明我是个Model。
    /// </summary>
    public interface IModel
    {

    }
    /// <summary>
    /// 该类将承接默认模型。
    /// </summary>
    class DefaultModels
    {
        public Dictionary<Type, IModel> defaultModels = new Dictionary<Type, IModel>();
        public DefaultModels()
        {
            defaultModels.Add(typeof(IChessModel), new ChessModel());
            defaultModels.Add(typeof(ICardModel), new CardModel());
            defaultModels.Add(typeof(IEnemyModel), new EnemyModel());
            defaultModels.Add(typeof(IPlayerCoreModel), new PlayerCoreModel());
            defaultModels.Add(typeof(IInfoModel), new InfoModel());
        }
    }
    /// <summary>
    /// 该类为IOC容器，从它这里获取对象。实际上，它是一个中转站，负责将DefaultModels在合适的时机注入
    /// </summary>
    public class ModelContainer
    {
        static ModelContainer _instance;
        /// <summary>
        /// 默认采用的Model。
        /// </summary>
        static DefaultModels defaultModels;
        public static ModelContainer Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ModelContainer();
                }
                return _instance;
            }
        }
        Dictionary<Type, IModel> _models = new Dictionary<Type, IModel>();
        private ModelContainer()
        {

        }
        private static void Register<T>(T model) where T : IModel
        {
            if (!Instance._models.ContainsKey(typeof(T)))
            {
                Instance._models[typeof(T)] = (IModel)model;
            }
        }
        public static T Get<T>() where T : IModel
        {
            if (!Instance._models.ContainsKey(typeof(T)))
            {
                if (defaultModels == null)
                {
                    defaultModels = new DefaultModels();
                }
                Register((T)defaultModels.defaultModels[typeof(T)]);
            }
            return (T)(Instance._models[typeof(T)]);
        }
        public static void Unregister<T>()
        {
            if (Instance._models.ContainsKey(typeof(T)))
            {
                Instance._models.Remove(typeof(T));
            }
        }

    }
}
